package com.bruce.apkinstaller.install;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/1/17.
 */

public class ProductArgsInstallImpl extends AArgsInstallBaseImpl implements InArgsInstallImpl {
    private ArgsPackageInstall mARArgsPackageInstall;

    @Override
    public void install(String apkAbsolutePath, ArgsPackageInstall argsInstallListener) {
        ArgsInstallerModel.install(apkAbsolutePath, argsInstallListener);
    }

    @Override
    public void start() {
        start(mApklist.get(0));
    }

    @Override
    public void setInstallCallback(ArrayList<String> apklist, InstallStatusCallback installCallback) {
        mInstallCallback = installCallback;
        mApklist = apklist;
        mARArgsPackageInstall = new ArgsPackageInstall();
    }

    @Override
    protected void start(String apkfile) {
        install(apkfile, mARArgsPackageInstall);
    }

    @Override
    protected void setInstallCallback(InstallStatusCallback installCallback) {
        mInstallCallback = installCallback;
        mARArgsPackageInstall = new ArgsPackageInstall();
    }

    public class ArgsPackageInstall implements InArgsPackageInstallObserver {

        @Override
        public void packageInstalled(String pack) {
            mInstallCallback.success(pack);
            installNext();
        }

        @Override
        public void installProgress(String apkfile, int progress) {
            if (mApklist.size() > 1) {
                mInstallCallback.progress(apkfile, (int) (((double) finishedCount / (mApklist.size() - 1)) * 100));
            } else {
                mInstallCallback.progress(apkfile, 100);
            }
        }

        @Override
        public void installFailure(String apkfile, Object reson) {
            mInstallCallback.failure(apkfile, reson);
            installNext();
        }
    }
}
